﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.IO;
using System.Web;
using System.Threading.Tasks;
using System.Data;
using BartonExtension;
using ResponseObject;
using MySql.Data.MySqlClient;
using MySql.Data;
using SQLWrapper;
using Model.Weixin;
using Model.App;

using Services.Common;
using Services.User;

using Cache.Utils;
using Cache;

using Senparc.Weixin.MP.Entities.Request;
using Senparc.Weixin.MP.MessageHandlers;
using Senparc.Weixin.MP;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.Helpers;
using Senparc.Weixin.MP.MvcExtension;
using Senparc.Weixin.Exceptions;
using Senparc.Weixin.MP.AdvancedAPIs.OAuth;
using Senparc.Weixin;

namespace iShangMai.Controllers.Weixin
{
    public class OAuth2Controller : ApiController
    {
        [HttpGet]
        public ZKResponse GetAuthUrl(string url, string state)
        {
            ZKResponse response = new ZKResponse();
            response.userinfo = AccessTokenHelper.GetAuthUrl(state, url);
            return response;
        }
        [HttpGet]
        public async Task<ZKResponse> GetUserInfo(string intopenid,string token)
        {
            ZKResponse response = new ZKResponse();
            ISMUserinfo userinfo = CacheManager.instance.objectFromCache<ISMUserinfo>(intopenid);
            if (userinfo == null)
            {
                ISMServiceResult result = await ISMUserInfoHelper.GetUserInfoFromDB(intopenid, token);
                response.isProcessSuccess = result.isOperationSuccess;
                response.errMsg = result.errorMsg;
                response.userinfo = result.dbReturningState;
            }
            else
            {
                response.userinfo = userinfo;
            }
            return response;
        }
        [HttpGet]
        public async Task<ZKResponse> GetUserInfo(string token,string code,string state)
        {
            ZKResponse response = new ZKResponse();
            ISMUserinfo ism_userinfo = null;
            if (state.ToUpper().Contains("STATE"))
            {
                AccessToken acs_token = await AccessTokenHelper.GetAccessToken(state, code);
                if (acs_token == null)
                {
                    response.isProcessSuccess = false;
                    response.errMsg = "连接微信服务错误";
                    return response;
                }
                ism_userinfo = await ISMUserInfoHelper.GetUserInfo(acs_token, token);
            }
            else
            {
                ism_userinfo = CacheManager.instance.objectFromCache<ISMUserinfo>(state);
                if(ism_userinfo == null)
                {
                    ISMServiceResult result = await ISMUserInfoHelper.GetUserInfoFromDB(state, token);
                    if(result.isOperationSuccess)
                    {
                        ism_userinfo = (ISMUserinfo)result.dbReturningState;
                        ism_userinfo.CacheUserinfo(ism_userinfo.intopenid);
                    }
                }
                else
                {
                    if(!JSSDKAccess.loaded)
                    {
                        JSSDKAccess.Init();
                    }
                    ism_userinfo.ticket = JSSDKAccess.js_ticket;
                }
            }
            if (ism_userinfo == null)
            {
                response.isProcessSuccess = false;
                response.errMsg = "获取用户信息错误";
            }
            else
            {
                response.userinfo = ism_userinfo;
            }
            return response;
        }
    }
}
